Euler Problem 45

Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:

Triangle $T_n=n(n+1)/2$ 1, 3, 6, 10, 15, ...

Pentagonal $P_n=n(3n−1)/2$ 1, 5, 12, 22, 35, ...

Hexagonal $H_n=n(2n−1)$ 1, 6, 15, 28, 45, ...

It can be verified that T285 = P165 = H143 = 40755.

Find the next triangle number that is also pentagonal and hexagonal.


In [1]:
pentagon = 1
hexagon = 1
p_delta = 4
h_delta = 5
while hexagon < 10**15:
    if pentagon == hexagon:
        print(pentagon)
    if pentagon <= hexagon:
        pentagon += p_delta
        p_delta += 3
    elif pentagon > hexagon:
        hexagon += h_delta
        h_delta += 4


1
40755
1533776805
57722156241751

Note: All hexagonal numbers are also triangular, since $H_n = T_{2n-1}$.